﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activities;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace WFExtended.Samples.ServiceModel
{
    public abstract class AServiceHost<T> : IDisposable
        where T : class, new()
    {
        private ServiceHost _host = null;

        public AServiceHost()
        {
            _host = new ServiceHost(new T(), Uri);

            foreach (ServiceEndpoint endPoint in ServiceEndpoints)
                _host.AddServiceEndpoint(endPoint);
            _host.Open();
        }

        public abstract Uri Uri { get; }

        public abstract List<ServiceEndpoint> ServiceEndpoints { get; }

        public void Dispose()
        {
            if (_host != null && (_host.State == CommunicationState.Opened
             || _host.State == CommunicationState.Opening))
                _host.Close();
            _host = null;
        }
    }
}
